package com.awanp3.nimbus.base.service.impl;

import com.awanp3.nimbus.base.domain.BasePlant;
import com.awanp3.nimbus.base.domain.bo.BasePlantBo;
import com.awanp3.nimbus.base.domain.vo.BasePlantVo;
import com.awanp3.nimbus.base.mapper.BasePlantMapper;
import com.awanp3.nimbus.base.service.IBasePlantService;
import com.awanp3.nimbus.common.core.utils.MapstructUtils;
import com.awanp3.nimbus.common.core.utils.StringUtils;
import com.awanp3.nimbus.common.mybatis.core.page.TableDataInfo;
import com.awanp3.nimbus.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Collection;

/**
 * 工厂信息Service业务层处理
 *
 * @author 20014170
 * @date 2025-08-04
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class BasePlantServiceImpl implements IBasePlantService {

    private final BasePlantMapper baseMapper;

    /**
     * 查询工厂信息
     *
     * @param plantId 主键
     * @return 工厂信息
     */
    @Override
    public BasePlantVo queryById(Long plantId){
        return baseMapper.selectVoById(plantId);
    }

    /**
     * 分页查询工厂信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 工厂信息分页列表
     */
    @Override
    public TableDataInfo<BasePlantVo> queryPageList(BasePlantBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BasePlant> lqw = buildQueryWrapper(bo);
        Page<BasePlantVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的工厂信息列表
     *
     * @param bo 查询条件
     * @return 工厂信息列表
     */
    @Override
    public List<BasePlantVo> queryList(BasePlantBo bo) {
        LambdaQueryWrapper<BasePlant> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BasePlant> buildQueryWrapper(BasePlantBo bo) {
        LambdaQueryWrapper<BasePlant> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(BasePlant::getPlantId);
        lqw.eq(StringUtils.isNotBlank(bo.getPlantCode()), BasePlant::getPlantCode, bo.getPlantCode());
        lqw.like(StringUtils.isNotBlank(bo.getPlantName()), BasePlant::getPlantName, bo.getPlantName());
        lqw.eq(StringUtils.isNotBlank(bo.getPlantType()), BasePlant::getPlantType, bo.getPlantType());
        lqw.eq(StringUtils.isNotBlank(bo.getCompanyCode()), BasePlant::getCompanyCode, bo.getCompanyCode());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), BasePlant::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), BasePlant::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BasePlant::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增工厂信息
     *
     * @param bo 工厂信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(BasePlantBo bo) {
        BasePlant add = MapstructUtils.convert(bo, BasePlant.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPlantId(add.getPlantId());
        }
        return flag;
    }

    /**
     * 修改工厂信息
     *
     * @param bo 工厂信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(BasePlantBo bo) {
        BasePlant update = MapstructUtils.convert(bo, BasePlant.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BasePlant entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除工厂信息信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
